package br.com.ava.core.funciona;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import br.com.ava.core.infra.persistencia.AbstractDAO;

@SuppressWarnings("serial")
public class FuncionaDAO extends AbstractDAO<Funciona> {

	/** {@inheritDocs} */
	@Override
	protected Class<Funciona> getClassePersistente() {
		return Funciona.class;
	}

	protected Integer obterIdAtual() {
		Criteria criteria = createCriteria();
		criteria.setProjection(Projections.max("idFuncionario"));
		return (Integer) criteria.uniqueResult();
	}

	@SuppressWarnings("unchecked")
	@Transactional(isolation = Isolation.DEFAULT, rollbackFor = Exception.class)
	public Funciona buscarPorEmail(String email) {
		Funciona retorno = null;
		Criteria criteria = createCriteria();
		criteria.add(Restrictions.eq("email", email));
		List<Funciona> lista = criteria.list();
		if (lista != null && !lista.isEmpty()) {
			retorno = lista.get(0);
		}
		return retorno;
	}

}